import process from 'process'
import {isEmpty} from './utils'
import chalk from 'chalk'
import os from 'os'

export function isDebug(): boolean {
    return !isEmpty(process.env['FLOW_RUNNER_DEBUG'] as string)
}

function hiddenLogTimestamp(): boolean {
    return !isEmpty(process.env['FLOW_RUNNER_HIDDEN_LOG_TIMESTAMP'] as string)
}

export function debug(message: string, newLine = true): void {
    // callLogCommand("debug", {}, message)
    if (!isDebug()) {
        return
    }
    const debugMessage = prependTimestamp(
        `[DEBUG] ${escapeLogMessage(message)}`
    )
    process.stdout.write(`${debugMessage}${newLine ? os.EOL : ''}`)
}

export function info(message: string, newLine = true): void {
    const infoMessage = prependTimestamp(
        `[INFO] ${escapeLogMessage(message)}`
    )
    process.stdout.write(`${infoMessage}${newLine ? os.EOL : ''}`)
}

export function warning(message: string, newLine = true): void {
    // callLogCommand("warning", {}, message)
    const warningMessage = prependTimestamp(
        `[WARNING] ${escapeLogMessage(message)}`
    )
    process.stdout.write(`${chalk.yellow(warningMessage)}${newLine ? os.EOL : ''}`)
}

export function error(message: string, newLine = true): void {
    // callLogCommand("error", {}, message)
    const errorMessage = prependTimestamp(
        `[ERROR] ${escapeLogMessage(message)}`
    )
    process.stdout.write(`${redV1(errorMessage)}${newLine ? os.EOL : ''}`)
}

export function success(message: string, newLine = true): void {
    const warningMessage = prependTimestamp(
        `[SUCCESS] ${escapeLogMessage(message)}`
    )
    process.stdout.write(`${chalk.green(warningMessage)}${newLine ? os.EOL : ''}`)
}

export function infoCyan(message: string, newLine = true): void {
    const warningMessage = prependTimestamp(
        `[INFO] ${escapeLogMessage(message)}`
    )

    process.stdout.write(`${chalk.cyan(warningMessage)}${newLine ? os.EOL : ''}`)
}

function prependV1Timestamp(message: string) {
    const now = timestampV1()
    return `${now} ${message}`
}

export function infoV1(message: string) {
    const errorMessage = prependV1Timestamp(
        `[INFO] ${escapeLogMessage(message)}`
    )
    process.stdout.write(`${errorMessage}${os.EOL}`)
}

export function errorV1(message: string) {
    const errorMessage = prependV1Timestamp(
        `[ERROR] ${escapeLogMessage(message)}`
    )
    process.stdout.write(`${redV1(errorMessage)}${os.EOL}`)
}

export function redV1(message: string) {
    return chalk.ansi256(196)(message)
}

export function warnV1(message: string) {
    const errorMessage = prependV1Timestamp(
        `[WARNING] ${escapeLogMessage(message)}`
    )
    process.stdout.write(`${chalk.yellow(errorMessage)}${os.EOL}`)
}

export function successV1(message: string) {
    const errorMessage = prependV1Timestamp(
        `[SUCCESS] ${escapeLogMessage(message)}`
    )
    process.stdout.write(`${chalk.green(errorMessage)}${os.EOL}`)
}

function prependTimestamp(rawMsg: string): string {
    if (hiddenLogTimestamp()) {
        return rawMsg
    }

    const now = timestamp()
    return `${now} ${rawMsg}`
}

function pad(n: number) {
    return n < 10 ? '0' + n : n
}

function timestampV1() {
    const d = new Date()
    const colon = ':'
    return ('[' +
        pad(d.getHours()) +
        colon +
        pad(d.getMinutes()) +
        colon +
        pad(d.getSeconds()) + ']'
    )
}

function timestamp() {
    const d = new Date()
    const dash = '-'
    const colon = ':'
    return (
        d.getFullYear() +
        dash +
        pad(d.getMonth() + 1) +
        dash +
        pad(d.getDate()) +
        ' ' +
        pad(d.getHours()) +
        colon +
        pad(d.getMinutes()) +
        colon +
        pad(d.getSeconds())
    )
}

function escapeLogMessage(rawMsg: string): string {
    return rawMsg
        .replace('/\r/g', '%0D')
        .replace('/\n/g', '%0A')
        .replace('/%/g', '%25')
}

export default {
    isDebug,
    debug,
    warning,
    info,
    error,
    errorV1,
    infoV1,
    warnV1,
    successV1
}
